package com.yu1996.gulimall.search.controller;

import com.yu1996.common.exception.BizCodeEnume;
import com.yu1996.common.to.es.SkuEsModel;
import com.yu1996.common.utils.R;
import com.yu1996.gulimall.search.service.ProductSaveService;
import java.io.IOException;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticSaveController {

  @Autowired private ProductSaveService productSaveService;

  @PostMapping("product")
  public R productStatusUp(@RequestBody List<SkuEsModel> models) {
    boolean isUp;
    try {
      isUp = productSaveService.productSaveService(models);
    } catch (IOException e) {
      log.error("ElasticSaveController商品上架发生错误{}", e);
      return R.error(
          BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
    }
    if (isUp) {
      return R.ok();
    } else {
      return R.error(
          BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
    }
  }
}
